function varargout = resample_data_flex(b,type,varargin)

N = size(varargin{1},1);

    %%%%%%%%%%%%%%%%%%%
    % verify length of varargin-elements
    %%%%%%%%%%%%%%%%%%%
    
for k = 1:length(varargin)
   aux = size(varargin{k},1);           
   if aux~=N
        error('different length');
   end
end

    %%%%%%%%%%%%%%%%%%%
    % NOW RESAMPLE
    %%%%%%%%%%%%%%%%%%%
       
switch lower(type)

   case {'with','with replacement'}

     u = rand(b,N);
     [~, pos_min] = min(u,[],2);
     for k = 1:length(varargin)
       aux = varargin{k};           
       varargout{k} = aux(pos_min,:);
     end
       
   case {'without','without replacement'}
     
     u = rand(N,1);
     
     for k = 1:length(varargin),
       aux = varargin{k};           
       data = [u aux];
       [~, pos_data] = sortrows(data,1);
       pos_draw = pos_data(1:b);
       varargout{k} = aux(pos_draw,:);
     end

   otherwise

      error('Please specify resampling method.');

end
  
        %
